package com.example.datedemo.service;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.datedemo.domain.MonthSample;
import com.example.datedemo.mapper.MonthSampleMapper;
import com.example.datedemo.service.dto.monthSample.MonthSampleAddDto;
import com.example.datedemo.service.dto.monthSample.MonthSampleDto;
import com.example.datedemo.service.dto.ResponseResult;
import com.example.datedemo.service.dto.monthSample.MonthSampleUpdateDto;
import com.example.datedemo.service.dto.weekSample.WeekSampleDto;
import com.example.datedemo.service.tools.StaticTools;
import org.apache.commons.io.IOUtils;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import static com.example.datedemo.service.tools.StaticTools.getModifyResult;

@Service
public class MonthSampleService {
    @Resource
    private MonthSampleMapper monthSampleMapper;

    // 显示所有Month
    public List<MonthSample> selectAll() {
        return monthSampleMapper.selectList(null);
    }

    // 根据id显示Month
    public ResponseResult selectById(String id) throws IOException {
        if (id == null || id == "") {
            return new ResponseResult(2, "null");
        }
        try{
            MonthSample entity = monthSampleMapper.selectById(id);
            return selectMergeDto(entity);
        }catch (NullPointerException e){
            return new ResponseResult(2,"没有该对象");
        }

    }

    // 根据projectId,monthNumber显示Month
    public ResponseResult selectByMonthNumber(int projectId, String monthNumber) throws IOException {
        if (monthNumber == null) {
            return new ResponseResult(2, "null");
        }

        QueryWrapper<MonthSample> monthSampleQueryWrapper = new QueryWrapper<>();
        monthSampleQueryWrapper.select().like("month_number", monthNumber).like("project_id", projectId);

        try {
            MonthSample entity = monthSampleMapper.selectOne(monthSampleQueryWrapper);
            return selectMergeDto(entity);
        }catch (NullPointerException e){
            return new ResponseResult(2,"没有该对象");
        }

    }

    // 新增Month
    public ResponseResult insertMonthSample(MonthSampleAddDto monthSampleAddDto) {
        MonthSample monthSample = new MonthSample();
        monthSample.setProjectId(monthSampleAddDto.getProjectId());

        monthSample.setMonthNumber(StaticTools.tranDate(monthSampleAddDto.getMonthNumber()));

        monthSample.setMonthText(monthSampleAddDto.getMonthText());

        try {
            return getModifyResult(monthSampleMapper.insert(monthSample), monthSample);
        }catch (DuplicateKeyException e){
            return new ResponseResult(3, "主键重复，该周数据可能已存在");
        }
    }

    // 更新Month
    public ResponseResult updateMonthSample(MonthSampleUpdateDto monthSampleUpdateDto) {
        MonthSample monthSample = new MonthSample();

        monthSample.setProjectId(monthSampleUpdateDto.getProjectId());
        monthSample.setMonthId(monthSampleUpdateDto.getMonthId());
        monthSample.setMonthNumber(StaticTools.tranDate(monthSampleUpdateDto.getMonthNumber()));

        monthSample.setMonthText(monthSampleUpdateDto.getMonthText());

        return getModifyResult(monthSampleMapper.updateById(monthSample), monthSample);
    }

    // 根据id删除Month
    public ResponseResult deleteMonthSampleById(String id) {
        int deleteNum = monthSampleMapper.deleteById(id);
        if (deleteNum == 0) {
            return new ResponseResult(0, "删除失败 或 无此id对象");
        }
        return new ResponseResult(1, "成功");
    }

    // 查询-整合Dto
    public ResponseResult selectMergeDto(MonthSample monthSample) throws IOException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");

        MonthSampleDto monthSampleDto = new MonthSampleDto();
        monthSampleDto.setProjectId(monthSample.getProjectId());
        monthSampleDto.setMonthId(monthSample.getMonthId());
        monthSampleDto.setMonthNumber(dateFormat.format(monthSample.getMonthNumber()));
        monthSampleDto.setMonthText(monthSample.getMonthText());

        int projectId = monthSample.getProjectId();

        String month = dateFormat.format(monthSample.getMonthNumber());

        // "/week/getWeekByMonth/{id}/{month}"
        String url = "http://localhost:8080/week/getWeekByMonth/" + projectId + "/" + month;
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(httpGet);
        HttpEntity httpEntity = response.getEntity();

        String data = IOUtils.toString(httpEntity.getContent());

        List<WeekSampleDto> list = JSON.parseArray(data, WeekSampleDto.class);

        monthSampleDto.setWeekSamples(list);

        httpClient.close();

        return new ResponseResult(1, "成功", monthSampleDto);
    }
}
